package org.toycode.logindemo.application.tapestry.pages;

import java.util.List;
import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.Errors;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.corelib.components.Label;
import org.apache.tapestry5.corelib.components.Loop;
import org.apache.tapestry5.corelib.components.OutputRaw;
import org.apache.tapestry5.corelib.components.Select;
import org.apache.tapestry5.corelib.components.TextField;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.toycode.logindemo.application.tapestry.util.SimpleSelectModel;
import org.toycode.logindemo.application.util.EjbUtil;
import org.toycode.logindemo.service.SolrServiceRemote;

/**
 * This class provides query for users.
 *
 * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
 * @version 1.0.0.2, Aug 28, 2009
 */
public class Search {

    /**
     * errors for search form.
     */
    @Component
    private Errors errors;
    /**
     * entity select.
     */
    @Component(parameters = {"label=message:entity", "value=entity",
                             "model=entityModel"})
    private Select entitySelect;
    /**
     * entity property select. 
     */
    @Component(parameters = {"label=message:prop", "value=prop",
                             "model=propModel"})
    private Select propSelect;
    /**
     * message for name filed validation.
     */
    @Component(parameters = {"for=keyField"})
    private Label keyLabel;
    /**
     * query key field.
     */
    @Component(parameters = {"label=message:key", "value=key",
                             "validate=required"})
    private TextField keyField;
    /**
     * query entity.
     */
    @Property
    private String entity = "User";
    /**
     * query property. 
     */
    @Property
    private String prop;
    /**
     * query key.
     */
    @Property
    private String key;
    /**
     * search form.
     */
    @Component
    private Form searchForm;
    /**
     * search results.
     */
    @Component(parameters = {"source=resultList", "value=rst"})
    private Loop results;
    /**
     * logger.
     */
    private Logger logger = LoggerFactory.getLogger(Search.class);
    /**
     * search service.
     */
    private SolrServiceRemote solrService =
            EjbUtil.getEjb(SolrServiceRemote.class);
    /**
     * messages.
     */
    @Inject
    private Messages messages;
    /**
     * a list of query results 
     */
    @Property
    @Persist
    private List<String> resultList;
    /**
     * a query result row in loop.
     */
    @Component(parameters={"value=rst"})
    private OutputRaw row;

    @Property
    private String rst;

    /**
     * Search form event handler method.
     *
     * @return always returns <code>null</code>
     */
    public Object onSuccessFromSearchForm() {
        logger.debug("entity[" + entity + "], prop[" + prop +
                     "], key[" + key + "]");
        if (solrService == null) {
            searchForm.recordError(messages.get("remote-connect-exception"));
            return null;
        }

        resultList = solrService.search(entity, prop, key);
//        List<String> results = solrService.search("*:*");
        if (resultList == null) {
            searchForm.recordError(keyField, messages.get("not-found"));
            return null;
        }
        for (String result : resultList) {
            logger.debug(result);
        }

        return null;
    }

    public SelectModel getEntityModel() {
        return new SimpleSelectModel(solrService.getEntityMap());
    }

    public SelectModel getPropModel() {
        return new SimpleSelectModel(solrService.getPropsByEntityName(entity));
    }
}
